package com.gosophia.conferenceManagement.summary.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.DozerChangeUtil;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.summary.dto.SendSummaryJobDataDTO;
import com.gosophia.conferenceManagement.summary.entity.SendSummaryJobData;
import com.gosophia.conferenceManagement.summary.service.SendSummaryService;
import com.gosophia.conferenceManagement.summary.ws.SendSummaryWebService;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;

@Service("sendSummaryWebService")
@Transactional
public class SendSummaryWebServiceImpl implements SendSummaryWebService{

    @Autowired
    private SendSummaryService sendSummaryService;
    @Autowired
    private TaskManagementService taskManagementService;
    
    @Autowired
    private UserManagementService userManagementService;
    
    private Mapper dozer = new DozerBeanMapper();
    @Override
    public void HandleConferenceTurningSummary(Long jobId, Long operatorId,
            SendSummaryJobDataDTO sendSummaryJobDataDTO)
            throws BusinessException {
        Assert.notNull(jobId, "jobId不能为null");
        Assert.notNull(sendSummaryJobDataDTO, "jobData不能为null");
        Assert.notNull(operatorId,"操作人Id不能为空");
        
        SendSummaryJobData sendSummaryJobData = new SendSummaryJobData();
        Document document=new Document();
        document.setDocumentPath(sendSummaryJobDataDTO.getOnlineDocumentDTO().getDocumentPath());
        document.setDocumentVersion(sendSummaryJobDataDTO.getOnlineDocumentDTO().getDocumentVersion());
        sendSummaryJobData.setOnlineDocument(document);
         //附件
         List<Document> attachments = new ArrayList<Document>();
         if(sendSummaryJobDataDTO.getAttachmentsDTOs()!=null){
             for (DocumentDTO d : sendSummaryJobDataDTO.getAttachmentsDTOs()) {
                Document doc = new Document();
                doc = dozer.map(d, Document.class);
                attachments.add(doc);
            }
         }
         //接收人
         if(sendSummaryJobDataDTO.getRecipientIds()!=null){
             List<UserInfo> recipients = new ArrayList<UserInfo>();
             for(Long id:sendSummaryJobDataDTO.getRecipientIds()){
                 UserInfo userInfo = userManagementService.findUserInfoByUserInfoId(id);
                 recipients.add(userInfo);
             }
             sendSummaryJobData.setRecipient(recipients);
         }
         sendSummaryJobData.setAttachments(attachments);
         sendSummaryJobData.setTurningAction(sendSummaryJobDataDTO.getTurningAction());
         sendSummaryService.HandleConferenceTurning(jobId, operatorId, sendSummaryJobData);
        
    }
    
    @Override
    public SendSummaryJobDataDTO findSendSummaryJobDataByJobDataId(Long jobId) throws BusinessException {
        Assert.notNull(jobId,"jobId不能为null");
        
        SendSummaryJobData sendSummaryJobData =(SendSummaryJobData)taskManagementService.findJobDataDetail(jobId, SendSummaryJobData.class);
        
        SendSummaryJobDataDTO sendSummaryJobDataDTO = null;
        if(sendSummaryJobData!=null){
            
            sendSummaryJobDataDTO= DozerChangeUtil.changeBeforeToAfter(sendSummaryJobData, SendSummaryJobDataDTO.class);
            
            
            if(sendSummaryJobData.getJob()!=null ){
                JobDTO jobDTO = DozerChangeUtil.changeBeforeToAfter(sendSummaryJobData.getJob(), JobDTO.class);
                sendSummaryJobDataDTO.setJobDTO(jobDTO);
                //处理人
                if( sendSummaryJobData.getJob().getOperator()!=null){
                    UserInfoDTO operatorDTO = DozerChangeUtil.changeBeforeToAfter(sendSummaryJobData.getJob().getOperator(), UserInfoDTO.class);
                    sendSummaryJobDataDTO.getJobDTO().setOperatorDTO(operatorDTO);
                }
            }
            //在线文档
            if(sendSummaryJobData.getOnlineDocument()!=null){
                DocumentDTO onlineDocumentDTO = DozerChangeUtil.changeBeforeToAfter(sendSummaryJobData.getOnlineDocument(), DocumentDTO.class);
                sendSummaryJobDataDTO.setOnlineDocumentDTO(onlineDocumentDTO);
            }
            //附件
            List<DocumentDTO> attachmentsDTOs = new ArrayList<DocumentDTO>();
            if(sendSummaryJobData.getAttachments()!=null && sendSummaryJobData.getAttachments().size()!=0){
                for(Document d:sendSummaryJobData.getAttachments()){
                    DocumentDTO attachmentDTO = DozerChangeUtil.changeBeforeToAfter(d, DocumentDTO.class);
                    attachmentsDTOs.add(attachmentDTO);
                }
                sendSummaryJobDataDTO.setAttachmentsDTOs(attachmentsDTOs);
            }
            //邮件接收人
            List<UserInfoDTO> recipientUserInfos = new ArrayList<UserInfoDTO>();
            if(sendSummaryJobData.getRecipient()!=null && sendSummaryJobData.getRecipient().size()!=0){
                for(UserInfo u:sendSummaryJobData.getRecipient()){
                    UserInfoDTO userInfoDTO = DozerChangeUtil.changeBeforeToAfter(u, UserInfoDTO.class);
                    recipientUserInfos.add(userInfoDTO);
                }
                sendSummaryJobDataDTO.setRecipientUserInfos(recipientUserInfos);
            }
            
        }
        return sendSummaryJobDataDTO;
    }

}
